package com.wyy.test

import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

import java.security.SecureRandom
import java.util.concurrent.CompletableFuture
import java.util.stream.Stream

/**
 * @Date: 2021/8/18
 * @Author: wyy
 */
class GroovyTest {

    @Test
    void test1() {
        def collect = (1..10).collect({ UUID.randomUUID() })
        println "collect = $collect"

        int count = count(collect.size())
        List<Integer> result = random(collect, count)
        println "result = $result"
    }

    @Test
    void testCompletableFuture() {
        CompletableFuture<String> completableFuture = new CompletableFuture<>();
        completableFuture.thenAccept {res->
            println res
        }
        completableFuture.complete("333")
        Thread.currentThread().join(1000)
    }

    @Test
    void testNull() {
        String str = "123"
        str && {print str.size()}
    }

    private int count(int max) {
        new SecureRandom().nextInt(max) + 1
    }

    private <T> List<T> random(List<T> list, int count = new SecureRandom().nextInt(list.size()) + 1) {
        List<T> copyList = new ArrayList<>(list)
        SecureRandom random = new SecureRandom()
        T temp

        count.times {
            int index = random.nextInt(copyList.size() - it) - 1
            println "index = $index"
            temp = copyList[index]
            copyList[index] = copyList[copyList.size() - it - 1]
            copyList[copyList.size() - it - 1] = temp
            println "copyList = $copyList"
        }
        return copyList[-1..-count]
    }

    @Test
    void testTree() {
        def node = new Node(
                id: 1,
                children: [
                        new Node(id: 2, children: [new Node(id: 4), new Node(id: 6)]),
                        new Node(id: 3, children: [new Node(id: 5), new Node(id: 7)])
                ]
        )

        def stream = Stream.of()


    }

    private Stream<Node> list(Node node) {
        return node.getChildren().parallelStream();
    }

    @Test
    @Disabled
    void testFile() {
        File file = new File("/Users/wyy/Downloads/access/etc_passwd.log")
        File file52 = new File("/Users/wyy/Downloads/access/etc_passwd-52.log")
        if (!file52.exists()) {
            file52.createNewFile()
        }
        List<String> lines = file.readLines()
        lines[0].split(" ")
        lines.each { line ->
            String[] split = line.split(" ")
            String path = split[8]
            String method = split[7].substring(1)

            String cmd = "curl -v -X ${method} 10.0.7.244$path"
            String cmd1 = "curl -v -X ${method} '10.0.7.244$path'"
            String res = cmd.execute().text
            file52.append(cmd1)
            file52.append("\n")
            file52.append(res)
            file52.append("\n" * 3)
        }
        println 1
    }

    @Test
    void testFileName() {
        int idx = 1
        int len = 1;
        while (idx ++ < 10) {
            String name = "a"*len++
            println "name = $name"
            String cmd = "cp /Users/wyy/temp/demo.zip /Users/wyy/temp/${name}.zip"
            cmd.execute()
        }
    }
}
